﻿using MainService.ZYPT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MainService.ZYPT_ZYLB;
using MainService.ZYPT_MS;
using MainService.ZYPT_ZJ;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
namespace MainService
{
    public static class ZYPTServer
    {
        public static string GetServerUrl(string url)
        {
            int index = url.IndexOf('/', 7);
            url = url.Substring(index);
            ServerAddress sa = MyServer.ServerConfig.ZYPT;
            return "http://" + sa.ServerIP + ":" + sa.ServerPort + url;
        }
        /// <summary>
        /// 获取所有资源类别
        /// </summary>
        /// <returns></returns>
        public static ZYLB GetZYLB() 
        {
            ResourceTypeService rts = new ResourceTypeService();
            rts.Url =  GetServerUrl(rts.Url);
            string xml = rts.qryITResourceType();
            XmlSerializer xs = new XmlSerializer(typeof(ZYLB));
            byte[] bytes = Encoding.UTF8.GetBytes(xml);
            ZYLB zylb = null;
            using(Stream sm=new MemoryStream(bytes))
            {
                zylb = (ZYLB)xs.Deserialize(sm);
            }
            return zylb;
        }
        /// <summary>
        /// 获取所有模式
        /// </summary>
        /// <returns></returns>
        public static MoShi GetMoShi()
        {
            ResourceModeService rts = new ResourceModeService();
            rts.Url = GetServerUrl(rts.Url);
            string xml = rts.qryAllResourceModel();
            XmlSerializer xs = new XmlSerializer(typeof(MoShi));
            byte[] bytes = Encoding.UTF8.GetBytes(xml);
            MoShi moshi = null;
            using (Stream sm = new MemoryStream(bytes))
            {
                moshi = (MoShi)xs.Deserialize(sm);
            }
            return moshi;
        }
        /// <summary>
        /// 获取所有组件
        /// </summary>
        /// <param name="resourceType"></param>
        public static ZJS GetZuJian(string resourceType) 
        {
            ResourceCompentService rcs = new ResourceCompentService();
            rcs.Url =  GetServerUrl(rcs.Url);
            string xml = rcs.qryCompByResourceType(resourceType);
            XmlSerializer xs = new XmlSerializer(typeof(ZJS));
            ZJS zjs = null;
            using (Stream sm = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                zjs = (ZJS)xs.Deserialize(sm);
            }
            return zjs;
        }
    }
}
